package com.ruitu.mall_98_1.product.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.beanu.arad.Arad;
import com.beanu.arad.base.my_base.base_list.ListBaseAdapter;
import com.beanu.arad.base.my_base.base_list.SuperViewHolder;
import com.beanu.arad.utils.Logg;
import com.ruitu.mall_98_1.R;
import com.ruitu.router_module.bean.EventModel;
import com.ruitu.router_module.bean.product.Sku;
import com.ruitu.router_module.bean.product.Spec;

import java.util.ArrayList;
import java.util.List;

//可以随意选 但是有一点点bug
public class Spec2Apter extends ListBaseAdapter<Spec> {
    private List<Sku> skuList = new ArrayList<>();//套餐形式的规格
    public String mDataListJson = "";

    public Spec2Apter(Context context) {
        super(context);
    }

    public void setSkuList(List<Sku> skuList) {
        this.skuList = skuList;
    }

    @Override
    public int getLayoutId() {
        return R.layout.pro_item_spec;
    }

    @Override
    public void onBindItemHolder(SuperViewHolder holder, final int specPos) {
        TextView tv_name = holder.getView(R.id.tv_name);
        RecyclerView rcv_val = holder.getView(R.id.rcv_val);

        final Spec spec = mDataList.get(specPos);
        tv_name.setText(spec.getGoodsFeatureName());

        final List<Spec.SpecDetail> valList = spec.getGoodsFeatureVals();
        final ValAdapter valAdapter = new ValAdapter(mContext);
        GridLayoutManager manager = new GridLayoutManager(mContext, 3);
        rcv_val.setLayoutManager(manager);
        rcv_val.setAdapter(valAdapter);
        valAdapter.setDataList(valList);
        valAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int valPos) {
                if (valList.get(valPos).isAble()) {//点击的item可用 可以被选中
                    if (valList.get(valPos).isSelected()) {
                        valList.get(valPos).setSelected(false);
                        mDataList.get(specPos).setChoose(false);
                    } else {
                        mDataList.get(specPos).setChoose(true);//该值所在的规格选中
                        for (int h = 0; h < valList.size(); h++) {
                            if (h == valPos) {
                                valList.get(h).setSelected(true);//值选中
                            } else {
                                valList.get(h).setSelected(false);//值未选中
                            }
                        }
                    }

                    List<String> selSpecValId = new ArrayList<>();//存储选中的规格和值(格式 sId:vId)
                    List<String> selSpecValName = new ArrayList<>();//存储选中的规格和值名称
                    for (int i = 0; i < mDataList.size(); i++) {//遍历规格,处理所有选中的规格(存储)
                        Spec spec = mDataList.get(i);
                        if (spec.isChoose()) {//该规格已选中
//                            Logg.i("当前选中:" + spec.getGoodsFeatureName());
                            //遍历该选中规格的所有的值,找到选中的值
                            for (int j = 0; j < spec.getGoodsFeatureVals().size(); j++) {
                                Spec.SpecDetail val = spec.getGoodsFeatureVals().get(j);
                                if (val.isSelected()) {//选中了值
                                    selSpecValId.add(spec.getId() + ":" + val.getId());//已选中的规格和值
                                    selSpecValName.add(val.getGoodsFeatureName() + "  ");
                                }
                            }

                            StringBuilder selNames = new StringBuilder("");
                            for (String name : selSpecValName) {
                                selNames.append(name + ",");
                            }
//                            Logg.i("所有选中:" + selNames.toString());
                        }
                    }
                    for (int i = 0; i < mDataList.size(); i++) {//遍历所有规格,处理未选中的(处理是否可用)
                        Spec spec = mDataList.get(i);
                        if (!spec.isChoose()) {//该规格未选中
                            //需要根据已选择的规格和值,标记出此规格可选和不可选的值
                            loop_n:
                            for (int n = 0; n < spec.getGoodsFeatureVals().size(); n++) {
                                //遍历该规格的值,设置可用或不可用
                                Spec.SpecDetail val = spec.getGoodsFeatureVals().get(n);
                                String thisSV = spec.getId() + ":" + val.getId();
                                val.setAble(true);
                                List<String> ableSkuList = new ArrayList<>();//记录根据已选的规格值,所有可用的套餐
                                loop_l:
                                for (int l = 0; l < skuList.size(); l++) {//遍历skuList,匹配判断可用和不可用
                                    String sku = skuList.get(l).getSkuInfo();//一条套餐(s1Id:v1Id,s2Id:v2Id)
                                    loop_m:
                                    for (int m = 0; m < selSpecValId.size(); m++) {
                                        String selSV = selSpecValId.get(m);
                                        //根据所有选择的规格值,找出所有可用的套餐
                                        if (!sku.contains(selSV)) {
                                            continue loop_l;
                                        }
                                    }
                                    ableSkuList.add(sku);
                                }
                                for (int o = 0; o < ableSkuList.size(); o++) {//所有可用的套餐
                                    if (ableSkuList.get(o).contains(thisSV)) {
                                        val.setAble(true);
                                        continue loop_n;
                                    } else {
                                        val.setAble(false);
                                        if (val.isSelected()) {
                                            val.setSelected(false);
                                            spec.setChoose(false);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    //循环完成,得到所有选中的规格和值(s1Id:v1Id  s2Id:v2Id  s3Id:v3Id  ...)
                    String selValNames = "";//选择所有的值
                    for (int i = 0; i < selSpecValName.size(); i++) {
                        selValNames += selSpecValName.get(i);
                    }
                    Arad.bus.post(new EventModel(18111951, selValNames));//选择的结果

                    if (selSpecValId.size() >= mDataList.size()) {//说明都选完了
                        for (int i = 0; i < skuList.size(); i++) {
                            boolean isSkuId = true;//是否是选择的skuId
                            String sku = skuList.get(i).getSkuInfo();
                            for (int j = 0; j < selSpecValId.size(); j++) {
                                if (!sku.contains(selSpecValId.get(j))) {
                                    isSkuId = false;
                                }
                            }
                            if (isSkuId) {
                                Logg.i("规格选择完成的id = " + skuList.get(i).getId());
                                Arad.bus.post(new EventModel(18110951, skuList.get(i)));
                            }
                        }
                    } else {//说明还没有选完规格
                        Arad.bus.post(new EventModel(18110951, null));
                    }
                    notifyDataSetChanged();
                } else {
                    //点击的item不可用 不能被选中 点击灰色按键,什么都不用处理
                }
            }
        });
    }

    private class ValAdapter extends ListBaseAdapter<Spec.SpecDetail> {

        public ValAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.pro_item_spec_tag;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView tv_tag_name = holder.getView(R.id.tv_tag_name);
            LinearLayout ll_item = holder.getView(R.id.ll_item);//item布局

            Spec.SpecDetail detail = mDataList.get(position);
            tv_tag_name.setText(detail.getGoodsFeatureName());

            if (detail.isAble()) {
                if (detail.isSelected()) {
                    ll_item.setSelected(true);
                    tv_tag_name.setTextColor(mContext.getResources().getColor(R.color.text_red));
                } else {
                    ll_item.setSelected(false);
                    tv_tag_name.setTextColor(mContext.getResources().getColor(R.color.text_black));
                }
            } else {
                tv_tag_name.setTextColor(mContext.getResources().getColor(R.color.text_dark_gray));
                ll_item.setSelected(false);
            }
        }
    }
}
